![]() |
Java Database Programming with JDBC
by Pratik Patel Coriolis, The Coriolis Group ISBN: 1576100561 Pub Date: 10/01/96 |
Previous | Table of Contents | Next |
The format for specifying a data source is an extended Universal Resource Locator (URL). The JDBC URL structure is broadly defined as follows
jdbc:<subprotocol>:<subname>
where jdbc is the standard base, subprotocol is the particular data source type, and subname is an additional specification that can be used by the subprotocol. The subname is based solely on the subprotocol. The subprotocol (which can be odbc, oracle, etc.) is used by the JDBC drivers to identify themselves and then to connect to that specific subprotocol. The subprotocol is also used by the DriverManager to match the proper driver to a specific subprotocol. The subname can contain additional information used by the satisfying subprotocol (i.e. driver), such as the location of the data source, as well as a port number or catalog. Again, this is dependent on the subprotocols JDBC driver. JavaSoft suggests that a network name follow the URL syntax:
jdbc:<subprotocol>://hostname:port/subsubname
The mSQL JDBC driver used in this book follows this syntax. Heres the URL you will see in some of the example code:
jdbc:msql://mycomputer.com:1112/databasename
The DriverManager.getConnection method in the JDBC API uses this URL when attempting to start a connection. Remember that a valid driver must be registered with the JDBC DriverManager before attempting to create this connection (as I discussed earlier in the Registering and Calling JDBC Drivers section). The DriverManager.getConnection method can be passed in a Property object where the keys user, password, and even server are set accordingly. The direct way of using the getConnection method involves passing these attributes in the constructor. The following is an example of how to create a Connection object from the DriverManager.getConnection method. This method returns a Connection object which is to be assigned to an instantiated Connection class:
String url="jdbc:msql://mydatabaseserver.com:1112/databasename"; Name = "pratik"; password = ""; Connection con; con = DriverManager.getConnection(url, Name, password); // remember to register the driver before doing this!
Chapter 4 shows a complete example of how to use the DriverManager and Connection classes, as well as how to execute queries against the database server and get the results.
In an effort to close the gap between existing ODBC drivers for data sources and the emerging pure Java JDBC drivers, JavaSoft and Intersolv released the JDBC-ODBC Bridge. Note that there is a Java interface (hidden as a JDBC driver called JdbcOdbcDriver and found in the jdbc/odbc/ directory below) that does the necessary JDBC to ODBC translation with the native method library that is part of the JDBC-ODBC bridge package. Although Chapter 5 covers the inner workings of the Bridge, I would like to show you how to install it here. Once the Bridge is set up, the JDBC handles access to the ODBC data sources just like access to normal JDBC drivers; in essence, you can use the same Java code with either JDBC drivers or ODBC drivers that use the Bridgeall you have to do is change the JDBC URL to reflect a different driver.
There are three steps to installing the JDBC-ODBC Bridge. Youll need to get the package first. Look on the CD-ROM, or grab the latest version from JavaSofts Web site at http://splash.javasoft.com/jdbc.
The data sources for the ODBC driver and the drivers themselves must be configured before you can run Java programs that access them. Consult your platform documentation and ODBC servers documentation for specific information.
One of the great features of the Bridge is that it allows you to use existing data sources to start developing database-aware Java applications. And with Access, you dont even need a database server! In Chapter 11, I present the full source code for writing an application server that can use the JDBC-ODBC Bridge, the Access ODBC drivers that come with Access 95, and an Access database to develop Java applets that can interact with a database without having a database server.
To set up an Access database for ODBC, follow these steps (Im assuming that you are using Windows 95):
That is all you need to do to set up the ODBC data source. Now you can write Java applications to interact with the data source on the machine in which you performed the configuration; the ODBC driver is not directly accessible over the network. You can access the data source by using the name you supplied in Step 5. For example, the URL would be something like
jdbc:odbc:DataSourceName
and the statement
Class.forName("jdbc.odbc.JdbcOdbcDriver")
would load the JDBC-ODBC bridge.
The next chapter works through a complete example of using a JDBC driver. I use the mSQL driver to query an mSQL database server over the network. The JDBC driver can easily be changed to use an ODBC driver or another JDBC driver to connect to a different data source.
Previous | Table of Contents | Next |